package com.jd.basic.service.impl;

import com.jd.basic.mapper.BaseMapper;
import com.jd.basic.query.BaseQuery;
import com.jd.basic.service.IBaseService;
import com.jd.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// 查询的方法比较多，使用查询的事务配置
// 只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
// @Inherited 在@Transactional有这个注解表示可以被继承，而@Service不能被继承，子类必须写
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findOne(Long id) {
        return baseMapper.findOne(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void del(Long id) {
        baseMapper.del(id);
    }

    @Override
    public PageList<T> queryPage(BaseQuery baseQuery) {
        // 查询总条数
        Long count = baseMapper.queryCount(baseQuery);
        // 判断如果没有就直接返回空集合
        if (count == 0){
            return new PageList<>();
        }
        // 查询当前页数据
        List<T> list = baseMapper.queryData(baseQuery);
        // 分页数据封装成PageList
        return new PageList<>(count,list);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        baseMapper.patchDel(ids);
    }


}
